<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
      >
        <el-form-item label="APPID" prop="param.appId">
          <el-input
            v-model="queryParams.param.appId"
            placeholder="请输入APPID"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="应用名称" prop="param.appName">
          <el-input
            v-model="queryParams.param.appName"
            placeholder="请输入应用名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="公司名称" prop="param.companyName">
          <el-input
            v-model="queryParams.param.companyName"
            placeholder="请输入公司名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="param.contract">
          <el-input
            v-model="queryParams.param.contract"
            placeholder="请输入联系人"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="appKey" prop="param.appKey">
          <el-input
            v-model="queryParams.param.appKey"
            placeholder="请输入联系人"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['definition:class:upload']"
            >导入</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column
          type="index"
          label="排序"
          width="60"
          fixed="left"
          align="center"
        />>
        <el-table-column
          prop="appName"
          label="应用名称"
          width="180"
          fixed="left"
          align="center"
        />
        <el-table-column
          prop="appId"
          label="APPID"
          width="180"
          align="center"
        />
        <el-table-column
          prop="appKey"
          label="appKey"
          width="180"
          align="center"
        />
        <el-table-column
          prop="secret"
          label="SECRET"
          width="180"
          align="center"
        />
        <el-table-column
          prop="contract"
          label="联系人"
          width="180"
          align="center"
        />
        <el-table-column prop="companyName" label="公司名称" />
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column prop="status" label="状态" width="180" align="center">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_notice_status"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="140">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-tools"
              @click="handleCorrelation(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >关联接口</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改部门对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="888px"
      append-to-body
    >
      <el-form ref="form" :model="addForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="应用名称" prop="appName">
              <el-input
                v-model="addForm.appName"
                placeholder="请输入应用名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="APPID" prop="appId">
              <el-input
                v-model="addForm.appId"
                placeholder="请输入APPID"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="addForm.companyName"
                placeholder="请输入公司名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="联系人" prop="contract">
              <el-input
                v-model="addForm.contract"
                placeholder="请输入联系人"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="appKey" prop="appKey">
              <el-input
                v-model="addForm.appKey"
                placeholder="请输入应用appKey"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="SECRET" prop="secret">
              <el-input
                v-model="addForm.secret"
                placeholder="请输入应用SECRET"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="启用状态" prop="status">
              <!-- <el-input
                v-model="addForm.status"
                placeholder="请输入启用状态"
              ></el-input> -->
              <el-switch
                v-model="addForm.status"
                active-text="正常"
                inactive-text="暂停"
                active-value="0"
                inactive-value="1"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="应用描述" prop="description">
              <el-input
                type="textarea"
                :rows="2"
                v-model="addForm.description"
                placeholder="请输入应用描述"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 应用关联接口对话框 -->
    <el-dialog
      title="关联接口管理"
      :visible.sync="correlationShow"
      width="888px"
      append-to-body
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleCorrelationAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="correlationMultiple"
            @click="handleCorrelationDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="correlationData"
        style="width: 100%"
        @selection-change="handlecorrelationChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column type="index" label="序号" width="60" fixed="left" />
        <!-- <el-table-column prop="id" label="接口ID" width="180" >
        </el-table-column> -->
        <el-table-column prop="name" label="接口名称" fixed="left">
        </el-table-column>
        <el-table-column
          prop="categoryName"
          label="接口分类"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="uri" align="center" label="接口URI">
        </el-table-column>
        <el-table-column
          prop="method"
          label="请求方法"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.quality_api_method"
              :value="scope.row.method"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="code"
          label="请求CODE"
          align="center"
          width="180"
        >
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 关联接口新增对话框 -->
    <el-dialog
      title="关联接口新增"
      :visible.sync="correlationAddShow"
      width="1200px"
      append-to-body
    >
      <el-table
        :data="interfaceData"
        style="width: 100%"
        @selection-change="handleSelectionInterfaceChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column prop="sort" label="排序" width="60" fixed="left" />
        <!-- <el-table-column prop="id" label="接口ID" width="180" >
        </el-table-column> -->
        <el-table-column prop="name" label="接口名称" fixed="left">
        </el-table-column>
        <el-table-column
          prop="categoryName"
          label="接口分类"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="uri" align="center" label="接口URI">
        </el-table-column>
        <el-table-column
          prop="method"
          label="请求方法"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.quality_api_method"
              :value="scope.row.method"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="code"
          label="请求CODE"
          align="center"
          width="180"
        >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="correlationAddSubmit"
          >确 定</el-button
        >
        <el-button @click="correlationAddCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertApp,
  updateApp,
  deleteApp,
  listApp,
  listAppInterface,
  insertAppInterface,
  deleteAppInterface,
} from "@/api/collect/application";
import { listInterface } from "@/api/collect/interface";
export default {
  dicts: ["sys_notice_status", "quality_api_method"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          appId: "",
          appName: "",
          companyName: "",
          contract: "",
          appKey: "",
        },
      },
      props: {
        checkStrictly: true,
        label: "name",
        value: "id",
      },
      dataList: [],
      total: 0,
      treeData: [],
      title: "",
      addDialog: false,
      addForm: {
        name: "",
        status: "0",
      },
      rules: {
        appName: [
          { required: true, message: "请输入应用名称", trigger: "blur" },
        ],
        appId: [
          { required: true, message: "请输入应用APPID", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        contract: [
          { required: true, message: "请输入联系人", trigger: "blur" },
        ],
        appKey: [
          { required: true, message: "请输入应用appKey", trigger: "blur" },
        ],
        secret: [
          { required: true, message: "请输入应用SECRET", trigger: "blur" },
        ],
      },
      correlationShow: false,
      correlationParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          appId: "",
        },
      },
      correlationTotal: 0,
      correlationData: [],
      correlationAddShow: false,
      interfaceData: [],
      interfaceTotal: 0,
      correlationAddIds: [],
      correlationMultiple: true,
      correlationIds: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = { ...this.queryParams };
      listApp(param).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增应用
    handleAdd() {
      this.title = "新增应用";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const param = { ...this.addForm };
          // param.categoryId = "1786565141330726912";
          param.status = "0";
          if (param.id) {
            // 修改
            updateApp(param).then((res) => {
              this.$message.success("修改成功");
              this.cancel();
              this.handleQuery();
            });
          } else {
            // 新增
            insertApp(param).then((res) => {
              this.$message.success("新增成功");
              this.cancel();
              this.handleQuery();
            });
          }
        } else {
          return false;
        }
      });
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        name: "",
      };
      this.addDialog = false;
    },
    // 修改应用
    handleEdit(row) {
      this.title = "修改应用";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = { ...data };
      this.addDialog = true;
    },
    // 删除应用
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的应用？")
        .then(function () {
          // 删除
          return deleteApp({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
    // 关联接口页面
    handleCorrelation(row) {
      const id = row.id;
      this.correlationParams.param.appId = id;
      listAppInterface(this.correlationParams).then((res) => {
        console.log("res: ", res);
        this.correlationData = [...res.data.list];
        this.correlationTotal = res.data.total;
        this.correlationShow = true;
      });
    },
    // getlistAppInterface() {
    //   listAppInterface(this.correlationParams).then((res) => {
    //     console.log("res: ", res);
    //     this.correlationData = [...res.data.list];
    //     this.correlationTotal = res.data.total;
    //     this.correlationShow = true;
    //   });
    // },
    // 新增关联接口
    handleCorrelationAdd() {
      const params = {
        pageNum: 1,
        pageSize: 20,
        param: {},
      };
      listInterface(params).then((res) => {
        this.interfaceData = res.data.list;
        this.interfaceTotal = res.data.total;
      });
      this.correlationAddShow = true;
    },
    handleSelectionInterfaceChange(selection) {
      this.correlationAddIds = selection.map((item) => item.id);
    },
    // 确认添加接口
    correlationAddSubmit() {
      const params = {
        appId: this.correlationParams.param.appId,
        frequency: 10,
        interfaceIdList: this.correlationAddIds,
      };
      insertAppInterface(params).then((res) => {
        this.$message.success("添加成功");
        const param = {
          id: this.correlationParams.param.appId,
        };
        this.handleCorrelation(param);
      });
      this.correlationAddShow = false;
    },
    // 取消添加接口
    correlationAddCancel() {
      this.correlationAddIds = [];
      this.correlationAddShow = false;
    },
    // 关联接口选中事件
    handlecorrelationChange(selection) {
      this.correlationIds = selection.map((item) => item.id);
      this.correlationMultiple = !selection.length;
    },
    // 关联接口批量删除
    handleCorrelationDelete() {
      const that = this;
      this.$modal
        .confirm("是否确认删除当前选择的关联接口？")
        .then(function () {
          // 删除
          const param = {
            appId: that.correlationParams.param.appId,
            interfaceIdList: [...that.correlationIds],
          };
          return deleteAppInterface(param);
        })
        .then(() => {
          this.$message.success("删除成功");
          const param = {
            id: this.correlationParams.param.appId,
          };
          this.handleCorrelation(param);
        })
        .catch(() => {});
    },
  },
};
</script>
